package org.web.webdisplay.server.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.web.base.domain.ResultDO;
import org.web.base.domain.ViewResultHelper;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.base.domain.helper.ValidationHelper;
import org.web.base.domain.view.ViewResultDTO;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.ArrayList;

import org.web.webdisplay.sdk.dto.query.QueryUserInfoRequest;
import org.web.webdisplay.sdk.dto.UserInfoDTO;
import org.web.webdisplay.server.service.UserInfoService;
import org.web.webdisplay.sdk.biz.UserInfoBiz;

@Controller("userInfo")
@RequestMapping("userInfo")
public class UserInfoController {

    private static final Logger logger = LogManager.getLogger(UserInfoController.class);

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private UserInfoBiz userInfoBiz;

    @RequestMapping("selectUserInfoList")
    @ResponseBody
    public ViewResultDTO<List<UserInfoDTO>> selectUserInfoList(@RequestBody QueryUserInfoRequest queryUserInfoRequest) {
        ViewResultDTO<List<UserInfoDTO>> viewResultDO = new ViewResultDTO<>(true);
        try {
            // 查询对象校验, 如果调用BIZ层方法，则无需校验，因为BIZ自会进行校验。
            ValidationHelper.validateWithThrow(QueryUserInfoRequest.class, queryUserInfoRequest);
            ResultDO<List<UserInfoDTO>> selectResult = userInfoBiz.selectUserInfoList(queryUserInfoRequest);
            viewResultDO.setData(selectResult.getDomain());
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            viewResultDO = ViewResultHelper.buildViewResultByServiceException(e, null);
        }
        return viewResultDO;
    }

    @RequestMapping("saveUserInfo")
    @ResponseBody
    public ViewResultDTO saveUserInfo(@RequestBody UserInfoDTO userInfoDTO) {
        try {
            List<UserInfoDTO> list = new ArrayList<>();
            list.add(userInfoDTO);
            ResultDO<List<UserInfoDTO>> batchResultDO = userInfoBiz.batchSaveUserInfoList(list);
            ResultDO<UserInfoDTO> resultDO = new ResultDO<>();
            if (batchResultDO != null && batchResultDO.isSuccess()) {
                resultDO.setDomain(batchResultDO.getDomain().get(0));
                return ViewResultHelper.convertToViewResultByResultDO(resultDO, UserInfoDTO.class);
            } else {
                return ViewResultHelper.convertToViewResultByResultDO(batchResultDO, UserInfoDTO.class);
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ViewResultHelper.buildViewResultByServiceException(e, null);
        }
    }


    @RequestMapping("batchDeleteUserInfoList")
    @ResponseBody
    public ViewResultDTO batchDeleteUserInfoList(@RequestBody List<Integer> ids) {
        ViewResultDTO viewResultDTO = new ViewResultDTO(false);
        try {
            List<UserInfoDTO> list = new ArrayList<>();
            for (Integer id : ids) {
                UserInfoDTO userInfoDTO = new UserInfoDTO();
                userInfoDTO.setUserInfoId(id);
                //userInfoDTO.setIsDelete("Y");
                list.add(userInfoDTO);
            }
            // TDOD , check it, 二选一，逻辑删除还是物理删除。
            //userInfoBiz.batchSaveUserInfoList(list);
            userInfoBiz.batchDeleteUserInfoList(ids);
            viewResultDTO.setResult(true);
            viewResultDTO.setMsg("批量删除成功");
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ViewResultHelper.buildViewResultByServiceException(e, null);
        }
        return viewResultDTO;
    }

}
